package com.quick.send.contact.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;

import com.quick.send.contact.model.Contact;

public class QuickActivity extends Activity {

	public static final String DEBUG_TAG = "Quick Send Contact Log";
	public static final String PREFS_QUICK_SEND_CONTACTS = "PrefsQuickSendContacts";
	public static final String PREFS_SELECTED_CONTACTS = "PrefsSelectedContacts";
	public static final String PREFS_SELECTED_CONTACT_IDS = "PrefsSelectedContactIds";
	public static final String PREFS_RECEIVER_CONTACTS = "PrefsReceiverContacts";
	public static final String PREFS_RECEIVER_CONTACT_IDS = "PrefsReceiverContactIds";
	public static final String SENT = "SMS_SENT";
	public static final String DELIVERED = "SMS_DELIVERED";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	protected List<Contact> loadContactListData() {

		List<Contact> contactInfoList = new ArrayList<Contact>();
		Cursor people = null;
		try {
			String[] projection = new String[] { ContactsContract.Contacts._ID,
					ContactsContract.Contacts.DISPLAY_NAME };
			String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER
					+ " = '1' ";
			String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
			people = getContentProviderClient().query(
					ContactsContract.Contacts.CONTENT_URI, projection,
					selection, null, sortOrder);

			while (people.moveToNext()) {

				String contactId = people.getString(people
						.getColumnIndex(ContactsContract.Contacts._ID));

				int nameFieldColumnIndex = people
						.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

				String displayName = getResources().getString(
						R.string.no_display_name);
				if (nameFieldColumnIndex > -1) {
					displayName = people.getString(nameFieldColumnIndex);
				}

				Contact contact = new Contact();
				contact.setContactId(contactId);
				contact.setDisplayName(displayName);
				contact.setSelected(false);
				// Set list phone number later for performance
				// contact.setMobileNumbers(getPhoneNumber(contactId));
				contactInfoList.add(contact);
			}

		} catch (Exception e) {

			Log.e(DEBUG_TAG, "Failed to get contact list on phone", e);

		} finally {

			if (people != null) {
				people.close();
			}
			if (contactInfoList.size() == 0) {

				Toast.makeText(this,
						getResources().getString(R.string.no_contact_found),
						Toast.LENGTH_LONG).show();
			}

		}
		return contactInfoList;
	}

	protected List<String> getPhoneNumber(String contactId) {

		List<String> listMobileNumbers = new ArrayList<String>();
		String mobileNumber = getResources()
				.getString(R.string.no_phone_number);
		Cursor phones = null;
		try {
			String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
			String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
					+ " = " + contactId;
			phones = getContentProviderClient().query(
					ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
					projection, selection, null, null);

			if (phones.getCount() > 0) {

				while (phones.moveToNext()) {
					mobileNumber = phones
							.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

					listMobileNumbers.add(mobileNumber);
				}

			}
		} catch (Exception e) {
			Log.e(DEBUG_TAG, "Failed to get list phone number of contact id: "
					+ contactId, e);
		} finally {

			if (phones != null) {
				phones.close();
			}
		}
		return listMobileNumbers;
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	protected List<String> getEmailAddress(String contactId) {

		List<String> listEmails = new ArrayList<String>();
		String emailAddress = "";
		Cursor emails = null;
		try {
			String[] projection = new String[] { ContactsContract.CommonDataKinds.Email.ADDRESS };
			String selection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID
					+ " = " + contactId;
			emails = getContentProviderClient().query(
					ContactsContract.CommonDataKinds.Email.CONTENT_URI,
					projection, selection, null, null);

			if (emails.getCount() > 0) {

				while (emails.moveToNext()) {
					emailAddress = emails
							.getString(emails
									.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

					listEmails.add(emailAddress);
				}

			}
		} catch (Exception e) {
			Log.e(DEBUG_TAG, "Failed to get list email of contact id: "
					+ contactId, e);
		} finally {

			if (emails != null) {
				emails.close();
			}
		}
		return listEmails;
	}

	protected SharedPreferences getSharedPreferences() {

		return getSharedPreferences(PREFS_QUICK_SEND_CONTACTS,
				Context.MODE_PRIVATE);

	}

	protected void clearSharedPreferences() {

		Editor editor = getSharedPreferences().edit();
		editor.clear();
		editor.commit();
	}

	private ContentProviderClient getContentProviderClient() {

		ContentResolver contentResolver = this.getContentResolver();
		ContentProviderClient contentProviderClient = contentResolver
				.acquireContentProviderClient(ContactsContract.Contacts.CONTENT_URI);
		return contentProviderClient;
	}

	protected boolean isNetworkConnected() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			return false;
		} else {
			return true;
		}
	}

}
